<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>file/shell/smooth/resources/css/jquery.multiselect.css">
<script src="<?php echo base_url(); ?>file/shell/smooth/resources/scripts/jquery.multiselect.min.js"></script>

<div class="page-title">
  <h2>Create Sell In</h2>
</div>

<?php 
  $message = $this->session->flashdata('message');
  if ($message) : $div_class = $this->session->flashdata('div_class');
?>
  <div class="<?php $div_class; ?>">
    <?php echo $message; ?>
  </div>
<?php endif; ?>

<div class="page page-trans">
  
<?php 
  function backdate($param) {
    if ($param > 1) {
      echo '<select>';
      for ($i=0; $i < $param; $i++) { 
        echo '<option value="'.date("Y-m-d", strtotime("-".$i." day")).'">'.date("m/d/Y", strtotime("-".$i." day")).'</option>';
      }
      echo '</select>';
    }
    else 
    {
      echo '<input type="text" name="sell_in_date" id="date" placeholder="mm/dd/yyyy" value="'.date("m/d/Y").'">';
    }
  }

?>

<form action="<?php echo base_url();?>index.php/sell/save" method="post">
  <table class="trans-info">
    <tr>
      <td>Territory</td>
      <td><strong><?php echo $territory; ?></strong></td>
    </tr>
    <tr>
      <td>Tanggal Sell In</td>
      <td>
        <input type="text" name="sell_in_date" id="date" placeholder="yyyy-mm-dd" value="<?php echo date("Y-m-d")?>" readonly="readonly" required>
        <?php //backdate(2); ?>
      </td>
    </tr>
    <tr>
      <td>Nama Sales</td>
      <td>
        <select name="sales_id" id="sales-id">
          <option value=""></option>
            <?php foreach ($salesID as $key) : ?>
                <option value="<?php echo $key->user_id.';'.$key->user_group_id; ?>"><?php echo $key->user_id.' | '.$key->user_group_caption.' | '.$key->user_name; ?></option>
            <?php endforeach; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Pilih Partner</td>
      <td>
        <select name="partner_id" id="partner-id">
          <option value=""></option>
          <!-- Appened partner list goes here -->
        </select>
      </td>
    </tr>
    <tr>
      <td>Piilih Program</td>
      <td>
        <select name="program_id" id="program-id">
          <option value="1">1</option>
          <option value="1">1</option>
          <option value="1">1</option>
          <option value="1">1</option>
        </select>
      </td>
    </tr>
  </table>
</form>

</div>

<script type="text/javascript">
  $(document).ready(function() {
    $('#sales-id').change(function(){
      var user = $(this).val().split(';');
      $('#partner-id').empty(); $('#partner-id').append(new Option("Pilih Partner",""));
      $.post(
        // "<?php echo base_url();?>index.php/sell/get_partners/"+sales_id,
        "<?php echo base_url();?>index.php/trans/get_partners/"+user[1]+"/"+user[0],
        function(data) {
          $.each(data, function(key, val){
            $('#partner-id').append(new Option(val["channel_id"]+" | "+val["channel_name"],val["channel_id"]));
            });
        }, "json"
      );
    });

    // --------------------------------------------------------------------------------------

    $('#partner-id').change(function() {
      var user = $('#sales-id').val().split(';');
      var partner = $(this).val();

      $('#program-id').empty();
      $.post(
        "<?php echo base_url();?>index.php/trans/get_program/"+user[1]+"/"+$(this).val(),
        function(data) {
          $.each(data, function(key, val){
            $('#program-id').append(new Option(val["caption"],val["program_id"]) );
            });
        }, "json"
      );
      // $("#program-id").multiselect();
      $("#program-id").attribute("multiple","multiple");
    });

    // --------------------------------------------------------------------------------------

    

  });
</script>